home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / win / steph1b0.zip / DEMO.C next >
C/C++ Source or Header  |  1994-10-30  |  14KB  |  526 lines

  1. /* DEMO.C
  2.  
  3.    Copyright (c) S Morphet, 1994
  4.  
  5.    Example program for STEPH library.  This program demonstrates some of
  6.    the features of Steph.  Steph has many other features, described in the
  7.    programmers manual.
  8.  
  9.    Compile with Microsoft C as a medium memory model program,
  10.    and link to STEPH_M.LIB.
  11.  
  12.    EDITOR.C is an example program showing a text editor working
  13.    with Steph.
  14.  
  15. */
  16.  
  17. /* You always need to include steph.h */
  18. /* You may need to change this line to set a path to steph.h */
  19.  
  20. #include <steph.h>
  21.  
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <conio.h>
  25. #include <string.h>
  26.  
  27. /******************************************************************/
  28.  
  29.  
  30. /* Functions in DEMO.C */
  31.  
  32. /* Various demo functions */
  33. void fn1( void );
  34. void fn2( void );
  35. void fn4( void );
  36.  
  37. /* Function to finish program */
  38. void fnexit( void );
  39.  
  40. /* Beep */
  41. void beep(void);
  42.  
  43. /* Functions to test f1 key in dialogue box */
  44. void passf1( void );
  45. void actf1( void );
  46.  
  47. /* Functions to perform system level key trapping */
  48. void _mysysfilter( void );
  49. void _mysysaction( void );
  50.  
  51. /* An initialise function */
  52. void myinit( void );
  53.  
  54. /******************************************************************/
  55.  
  56.  
  57. /* the MAIN function */
  58.  
  59. void main( void )
  60. {
  61.    /* Steph will try to use the current graphics mode */
  62.    /* Here we assume 16 colour text mode is already set. */
  63.  
  64.    /* Initialise Steph */
  65.    if ( !_steph_initialise( M_TEXT ) )
  66.    {
  67.       perror("Steph couldn't start in the current graphics mode.\n");
  68.       exit(0);
  69.    }
  70.  
  71.    /* set up menus and hooked functions */
  72.  
  73.    if( ! _menu_build( 0, "&Test", /* this is the menu title */
  74.          "&Dialogue",             /* a menu item */
  75.          "=",                     /* horizontal bar across menu */
  76.          "&Query",                /* another item */
  77.          "E&xit.",                /* another item */
  78.          NULL ) )                 /* NULL marks the end of the list */
  79.    {
  80.       perror("Unable to build menu 0.\n");
  81.       return;
  82.    }
  83.  
  84.    _menu_functions( 0,
  85.          (vfnptr)fn1,            /* attach our fn1 */
  86.          NOFN,                   /* can't hook to the horiz bar */
  87.          (vfnptr)fn4,            /* another fn */
  88.          (vfnptr)fnexit );       /* attach our exit function */
  89.  
  90.    _menu_helptext( 0,
  91.          "Here is text for the bar option",      /* help texts */
  92.          "Pop up a dialogue box to test",
  93.          NOTEXT,
  94.          "Function two doesn't do much",
  95.          "Exit the program" );
  96.  
  97.          
  98.    /* build another menu */
  99.  
  100.    if( ! _menu_build( 1, "^&Squeak",   /* the ^ makes this a rhs menu    */
  101.          "$S&quelch",                  /* $ means the item starts grey   */
  102.          "=",
  103.          "?&Toggle Me!",               /* ? means this item is checked   */
  104.          "Tic&kle",
  105.          NULL ) )
  106.    {
  107.       perror("Unable to build menu 1.\n");
  108.       return;
  109.    }
  110.  
  111.    _menu_functions( 1,                        /* attach the toggle func. */
  112.          NOFN, NOFN, _menu_toggle, NOFN );    /* to the checked item     */
  113.  
  114.    _menu_helptext( 1,
  115.          "This is the squeak menu",
  116.          "Squelchy",
  117.          NOTEXT,
  118.          "Snuffly",
  119.          "Tickly" );
  120.  
  121.          
  122.    /* these rest of the menus have no attached functions yet */
  123.  
  124.    if( ! _menu_build( 2, "&Whschkiput", "&item", NULL ) )
  125.    {
  126.       perror("Unable to build menu 2.\n");
  127.       return;
  128.    }
  129.    _menu_helptext( 2,
  130.          "The noise that a llama makes",
  131.          "The item" );
  132.  
  133.          
  134.    /* this whole menu is greyed */
  135.    
  136.    if( ! _menu_build( 3, "$&Quack", "Duck&1", "Duck&2", NULL ) )
  137.    {
  138.       perror("Unable to build menu 3.\n");
  139.    }
  140.    _menu_helptext( 3,
  141.          "More ducks, need more attack...",
  142.          "Top duck",
  143.          "Another duck" );
  144.  
  145.          
  146.    if( ! _menu_build( 4, "T&weet", "ite&m", NULL ) )
  147.    {
  148.       perror("Unable to build menu 4.\n");
  149.       return;
  150.    }
  151.    _menu_helptext( 4,
  152.          "This is the tweet menu",
  153.          "There is only one bird here" );
  154.  
  155.          
  156.    /* another on the right hand side */
  157.    
  158.    if( ! _menu_build( 5, "^&Baa", "item&1", "item&2", NULL) )
  159.    {
  160.       perror("Unable to build menu 5.\n");
  161.       return;
  162.    }
  163.    _menu_helptext( 5,
  164.          "This is the special wooly sheep menu",
  165.          "Sheep 1",
  166.          "Sheep 2" );
  167.  
  168.          
  169.    if( ! _menu_build( 6, "&Nibble", "Don't be &sad!", NULL ) )
  170.    {
  171.       perror("Unable to build menu 6.\n");
  172.       return;
  173.    }
  174.    _menu_helptext( 6,
  175.          "Nibble menu",
  176.          "Ashumbadiddly!" );
  177.  
  178.          
  179.    /* Set up pointer to our application initialisation function */
  180.    
  181.    _steph_initialfunction( myinit );
  182.  
  183.    
  184.    /* Set up the screen windows leaving all the hooks free */
  185.    /* i.e. Nothing happens in these windows. */
  186.    
  187.    _window_build( 0, WP_MAIN, 10, "0 The Title", NOFN, NOFN, NOFN );
  188.    _window_build( 1, WP_TOP, 2, "1 Topper than you!", NOFN, NOFN, NOFN );
  189.    _window_build( 2, WP_TOP, 2, "2 Top Window.", NOFN, NOFN, NOFN );
  190.    _window_build( 3, WP_BOT, 3, "3 Bottom Window.", NOFN, NOFN, NOFN );
  191.    _window_build( 4, WP_BOT, 3, "4 Right at the bottom.", NOFN, NOFN, NOFN );
  192.  
  193.    /* remember, only window zero, the main window is open by default */
  194.  
  195.    
  196.    /* fix the top two lines of window 2 */
  197.  
  198.    _window_setfixed( 2, 2 );
  199.    
  200.  
  201.    /* Set up dialogue boxes */
  202.   
  203.    /* A big dialogue to demo all the controls... */
  204.    /* This is dbox zero, centred on the screen, with ten controls. */
  205.    /* 60 chars wide, 20 chars deep */
  206.  
  207.    if( !_dbox_build( 0, DB_CENSCR, 60, 20, 10,
  208.          NOTEXT, "Help text for the dialogue" ) )
  209.    {
  210.       perror("Couldn't build dialogue box 0.\n");
  211.       return;
  212.    }
  213.    
  214.    /* Give it some horiz bars, and make dbox1 a dependant */
  215.  
  216.    _dbox_setbars( 0, 6, _NO_BAR   );
  217.    _dbox_setdependants( 0, 1, _NO_DEPENDANT );
  218.  
  219.  
  220.    /* remember - lists, combo lists and text should be in DYNAMIC memory */
  221.  
  222.    /* set up some buttons and things... */
  223.  
  224.    /* a non interactive label */
  225.    _dbox_c_label( 0, 0, 2, 2, "This is a label." );
  226.  
  227.    /* a button, the dialogue closes when it is pressed */
  228.    _dbox_c_button( 0, 1, 2, 4, 9, "&OK", EXIT, NOFN );
  229.  
  230.    /* a button, closes the dialogue without restoring data */
  231.    _dbox_c_button( 0, 2, 2, 5, 9, "&Cancel", ESCAPE, NOFN );
  232.  
  233.    /* a button, opens another dialogue via fn2 */
  234.    _dbox_c_button( 0, 3, 2, 8, 9, "&Another", NOEXIT, (vfnptr)fn2 );
  235.  
  236.    /* a check box, begins checked */
  237.    _dbox_c_check( 0, 4, 2, 10, "Chec&k me!", 1 , NOFN   );
  238.  
  239.    /* a set of three radio buttons -  long line ->> */
  240.    _dbox_c_radio( 0, 5, _s_buildradiolist("\x1b\x01\x00\x00\x00Text&1\0\x1b\x02\x00\x00\x00T&wo\0\x1b\x03\x00\x00\x01Three&3\0\0"), 0, NOFN   );
  241.  
  242.    /* an editable text box */
  243.    _dbox_c_text( 0, 6, 0x1b, 5, 12, "&Text box",
  244.       (char*)_s_strdup("TTKFaster4"), NOFN, NOFN );
  245.  
  246.    /* a vertical list, empty so Steph will ignore it... */
  247.    _dbox_c_list( 0, 7, 0x1b, 8, 20, 4, 6, DBL_VSCROLL, "&Vertical List",
  248.          _s_buildlist(""), EXIT, NOFN, NOFN   );
  249.  
  250.    /* a horizontal list, not empty -  long line >>>> */
  251.    _dbox_c_list( 0, 8, 2, 13, 20, 2, 6, DBL_HSCROLL, "&Horizontal List",
  252.          _s_buildlist("Atem1\0Btem2\0Ctem3\0Dtem4\0Etem5\0Ftem6\0Atem7\0Htem8\0Item9\0"), NOEXIT, NOFN, NOFN   );
  253.  
  254.    /* a combo box, exits dialogue when selection made */
  255.    _dbox_c_combo( 0, 9, 0x1b, 17, 10, 3, 3, "Co&mbo",
  256.          _s_buildlist("Item1\0Item2\0Item3\0Item4\0"), EXIT, NOFN, NOFN );
  257.  
  258.          
  259.    /* Keystroke filter and action function for dialogue zero */
  260.  
  261.    _dbox_set_global_keys( 0, passf1, actf1 );
  262.  
  263.    
  264.    /* Another dialogue.  A dependant of dbox 0. */
  265.  
  266.    if( !_dbox_build( 1, DB_CENSCR, 30, 12, 3,
  267.          NOTEXT, "Click OK to continue." ) )
  268.    {
  269.       perror("Couldn't build dialogue 1.\n");
  270.       return;
  271.    }
  272.    
  273.    _dbox_c_label( 1, 0, 2, 2, "Dialogue 1." );
  274.    _dbox_c_button( 1, 1, 2, 5, 6, "&OK", EXIT, NOFN );
  275.    _dbox_c_check( 1, 2, 2, 8, "&Data!", 0, NOFN );
  276.  
  277.    
  278.    /* A third dialogue - maybe you pressed F1 for online help? */
  279.  
  280.    if( !_dbox_build( 2, DB_CENSCR, 30, 6, 2, "Help", "Joking?" ) )
  281.    {
  282.       perror("Couldn't build dialogue 2.\n");
  283.       return;
  284.    }
  285.  
  286.    _dbox_c_label( 2, 0, 2, 2, "No Help available!" );
  287.    _dbox_c_button( 2, 1, 2, 4, 6, "&OK", EXIT, NOFN );
  288.  
  289.  
  290.    /* Set the speed bar buttons, and attach the beep function to
  291.       the rest of the bar. */
  292.  
  293.    _speed_button_set( 0, "ALT=Menu", TRUE, _menu_go );
  294.    _speed_button_set( 1, "One", TRUE, NOFN );
  295.    _speed_button_set( 2, "Another", TRUE, NOFN );
  296.    
  297.    _speed_clickfn( (vfnptr)beep );
  298.  
  299.  
  300.    /* Set up filter and action functions for system level keytraps */
  301.    
  302.    _window_setsyskey( (vfnptr)_mysysfilter, (vfnptr)_mysysaction );
  303.  
  304.    
  305.    /* Ask for one line of tool bar space */
  306.  
  307.    _toolbar_reserve( 1 );
  308.  
  309.  
  310.    /* Set up a button, a toggle and a combo on the tool bar */
  311.    
  312.    _toolbar_set_button( 0, 0, 2, "<Button>", NOFN );
  313.    _toolbar_set_toggle( 1, 0, 12, "<Toggle>", ON, NOFN, NOFN );
  314.  
  315.    /* this is a long line >>>> */
  316.    _toolbar_set_combo( 2, 0, 22, 10, 8, "Combo", _s_buildlist("One\0Two\0Three\0Four\0Five\0Six\0Seven\0Eight\0Nine\0Ten\0Eleven\0"), 0, NOFN );
  317.  
  318.  
  319.    /* give control to steph */
  320.  
  321.    _steph_go();
  322.  
  323.    /* if execution reaches this point, it is because no menus or windows
  324.       had been defined, and Steph refused to start. */
  325.       
  326.    /* The program usually ends when fnexit() (below) is called from
  327.       the menu */
  328. }
  329.  
  330.  
  331. /******************************************************************/
  332.  
  333. /* a function to make a beep */
  334.  
  335. void beep(void) { printf("\a"); }
  336.  
  337. /******************************************************************/
  338.  
  339. /* open dialogue box zero */
  340.  
  341. void fn1( void ) { _dbox_go( 0 ); }
  342.  
  343. /******************************************************************/
  344.  
  345. /* open dialogue box one */
  346.  
  347. void fn2( void ) { _dbox_go( 1 ); }
  348.  
  349. /******************************************************************/
  350.  
  351. /* open a query box, and report on the result with a flash box */
  352.  
  353. void fn4( void )
  354. {
  355.   char *text;
  356.  
  357.    /* build and open the query using dialogue 3 as a store */
  358.  
  359.    switch (_query_box( DB_CENSCR,
  360.          _s_buildlist("Some text\0Some more text\0"),
  361.          _s_buildlist( "Button1\0But2\0" ), "Helptext" ) )
  362.    {
  363.       case QB_NONE:
  364.       {
  365.          text =_s_buildlist("No button was pressed\0Press a key.\0");
  366.          break;
  367.       }
  368.       case 0:
  369.       {
  370.          text = _s_buildlist("The first button was pressed\0Press a key.\0");
  371.          break;
  372.       }
  373.       case 1:
  374.       {
  375.          text = _s_buildlist("The second button was pressed\0Press a key.\0");
  376.          break;
  377.       }
  378.  
  379.    }
  380.  
  381.    /* display the result */
  382.  
  383.    _flash_box( DB_CENSCR, text );
  384.  
  385.    /* wait for a key, then remove it from the buffer */
  386.    _s_wait_for_key();
  387.  
  388.    _flash_remove();
  389. }
  390.  
  391.  
  392. /******************************************************************/
  393.  
  394. /* an exit function like this is the only way out of Steph. */
  395.  
  396. void fnexit( void )
  397. {
  398.    _steph_closedown();
  399.    exit(0);
  400. }
  401.  
  402. /******************************************************************/
  403.  
  404. /* the initialise function, runs just after steph_go is called */
  405.  
  406. void myinit( void )
  407. {
  408.    /* open the other windows that we built */
  409.  
  410.    _window_open( 1, OPEN );
  411.    _window_open( 2, OPEN );
  412.    _window_open( 3, OPEN );
  413.    _window_open( 4, OPEN );
  414.  
  415.    /* make 2 the active window */
  416.    _window_make_active( 2 );
  417.  
  418.    /* put some text into window 2, make the window bigger to see it all */
  419.  
  420.    _wb_newline( 2 );
  421.    _wb_settext( 2, _s_strdup("Wombat.") );
  422.  
  423.    _wb_newline( 2 );
  424.    _wb_settext( 2, _s_strdup("Mongoose.") );
  425.  
  426.    _wb_newline( 2 );
  427.    _wb_settext( 2, _s_strdup("Aardvark.") );
  428.  
  429.    _wb_newline( 2 );
  430.    _wb_settext( 2, _s_strdup("Gerbil.") );
  431.  
  432.    _wb_newline( 2 );
  433.    _wb_settext( 2, _s_strdup("Cat.") );
  434.  
  435.    _wb_newline( 2 );
  436.    _wb_settext( 2, _s_strdup("Pigeon.") );
  437.  
  438.    _wb_newline( 2 );
  439.    _wb_settext( 2, _s_strdup("Other animals.") );
  440.  
  441.    /* insert a blank line after line 2 (Aardvark) */
  442.  
  443.    _wb_gotoline( 2, 2 );
  444.    _wb_newline(2);
  445.  
  446.    /* update the windows after having opened them etc. */
  447.  
  448.    _window_draw();
  449.    _window_refresh_all();
  450.  
  451. }
  452.  
  453.  
  454. /******************************************************************/
  455.  
  456. /* filter for system keystrokes - pass only F1:    00,59 dec   00,3b hex */
  457.  
  458. void _mysysfilter( void )
  459. {
  460.    /* reset the 'match found' flag */
  461.    _window_spec.syskeyflag = FALSE;
  462.  
  463.    /* set the flag if the key is F1 */
  464.    if( _window_spec.key1 == 0 )
  465.       if( _window_spec.key2 == 59 )
  466.          _window_spec.syskeyflag = TRUE;
  467. }
  468.  
  469.  
  470. /* open dialogue 2 if F1 is pressed */
  471. /* note system keystrokes are inhibited when dialogues are open */
  472.  
  473. void _mysysaction( void )
  474. {
  475.    switch( _window_spec.key1 )
  476.    {
  477.       case 0:
  478.          switch( _window_spec.key2 )
  479.          {
  480.             case 59:
  481.             {
  482.                _dbox_go( 2 );
  483.             }
  484.  
  485.             default:
  486.             {}
  487.          }
  488.  
  489.       default:
  490.       {}
  491.    }
  492. }
  493.  
  494. /******************************************************************/
  495.  
  496. /* dialogue keystroke filter for dialogue 0 */
  497. /* pass the F1 key only - c.f. _mysysfilter above */
  498.  
  499. void passf1( void )
  500. {
  501.    /* pass only f1 for dbx global keys */
  502.  
  503.    if( ( _dbox_spec.filter_key != 0 ) || ( _dbox_spec.filter_key2 != 59) )
  504.    {
  505.       _dbox_spec.filter_key = 0;
  506.       _dbox_spec.filter_key2 = 0;
  507.    }
  508. }
  509.  
  510.  
  511. /* open dialogue 1 if F1 pressed in dialogue */
  512. /* note system keystrokes are inhibited when dialogues are open */
  513.  
  514. void actf1( void )
  515. {
  516.    /* call a dbox if you press f1 in this dbox */
  517.    _dbox_go( 1 );
  518. }
  519.  
  520. /******************************************************************/
  521.  
  522. /* end of DEMO.C */
  523.  
  524. /* there are a number of other functions... */
  525. /* read the manual file STEPH.DOC, and see EDITOR.C for more information */
  526.